package com.example.rentingapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.rentingapp.MainActivity;
import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationEntity;
import com.example.rentingapp.enetity.InformationResponse;
import com.example.rentingapp.enetity.OrdersEntity;
import com.example.rentingapp.enetity.OrdersResponse;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class OrderDetailsActivity1 extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack, delete;
    private Button btn_confirm;
    private RelativeLayout rlButton;
    private Date date;
    private String pattern = "yyyy-MM-dd",c;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

    private TextView tvTenantName, tvHomeType, tvAddress, tvRent, tvCheckIn, tvCheckOut;
    private ImageView imgContract;
    private String orders_id, orders_tenant_id;
    private char check_orders;


    private List<OrdersEntity> mOrdersEntity = new ArrayList<>();
    private List<InformationEntity> mInformationEntity = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    orders_tenant_id = String.valueOf(mOrdersEntity.get(0).getOrders_tenant_id());
                    check_orders = mOrdersEntity.get(0).getCheck_orders();

                    tvAddress.setText(mOrdersEntity.get(0).getAddress());
                    tvHomeType.setText(String.valueOf(mOrdersEntity.get(0).getHome_type()));
                    tvRent.setText(String.valueOf(mOrdersEntity.get(0).getHouse_rent()));
                    date=mOrdersEntity.get(0).getCheck_in();
                    c=simpleDateFormat.format(date);
                    tvCheckIn.setText(c);
                    date=mOrdersEntity.get(0).getCheck_out();
                    c=simpleDateFormat.format(date);
                    tvCheckOut.setText(c);

                    if (check_orders == 'Y') {
                        rlButton.setVisibility(View.INVISIBLE);
                    }

                    getTenantName();//查找房客名字
                    break;
                case 1:
                    tvTenantName.setText(mInformationEntity.get(0).getName());//房客名字
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.search_myorder_details1;
    }

    @Override
    protected void initView() {
        ivBack = findViewById(R.id.iv_back);
        delete = findViewById(R.id.btn__delete);
        btn_confirm = findViewById(R.id.btn_order_confirm);
        rlButton = findViewById(R.id.rl_button);

        tvTenantName = findViewById(R.id.tv_tenant_name);
        tvHomeType = findViewById(R.id.tv_home_type);
        tvAddress = findViewById(R.id.tv_address);
        tvRent = findViewById(R.id.tv_rent);
        tvCheckIn = findViewById(R.id.tv_check_in);
        tvCheckOut = findViewById(R.id.tv_check_out);
        imgContract = findViewById(R.id.img_contract);
    }

    @Override
    protected void initData() {
        ivBack.setOnClickListener(this);//返回按钮
        delete.setOnClickListener(this);//删除按钮
        btn_confirm.setOnClickListener(this);//确认订单按钮
        orders_id = findByKey("orders_id");
        getOrderDetails();//查找订单
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                removeByKey("order_id");
                navigateTo(MyOrderActivity.class);
                finish();
                break;
            case R.id.btn__delete:
                changeOrdersStatus('N');
                navigateTo(MyOrderActivity.class);
                finish();
                break;
            case R.id.btn_order_confirm:
                changeOrdersStatus('Y');
                navigateTo(MyOrderActivity.class);
                finish();
                break;
        }
    }

    //查看生成的订单
    private void getOrderDetails() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("orders_id", orders_id);
        Api.config(ApiConfig.FIND_ORDERS_BY_ORDERS_ID, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                OrdersResponse response = gson.fromJson(res, OrdersResponse.class);
                if (response.getSuccess() == true) {
                    List<OrdersEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        mOrdersEntity = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("生成订单失败");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }

    private void getTenantName() {
        HashMap<String, Object> params2 = new HashMap<String, Object>();

        params2.put("information_user_id", orders_tenant_id);
        Api.config(ApiConfig.FIND_INFORMATION, params2).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationResponse response = gson.fromJson(res, InformationResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        mInformationEntity = list;
                        mHandler.sendEmptyMessage(1);
                    }
                } else {
                    showToastSync("未找到房客姓名");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }

    //改变订单状态
    private void changeOrdersStatus(char check_orders) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("orders_id", orders_id);
        params.put("check_orders", check_orders);
        Api.config(ApiConfig.POST_ORDERS_BY_ORDERS_ID_AND_CHECK, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.e("onSuccess", res);
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}
